package com.yuchen.bearrecord.service.impl;

import com.yuchen.bearrecord.config.RequestHeaderContext;
import com.yuchen.bearrecord.config.Result;
import com.yuchen.bearrecord.dto.UserDto;
import com.yuchen.bearrecord.dto.UserOpenidDto;
import com.yuchen.bearrecord.entity.User;
import com.yuchen.bearrecord.repository.UserRepository;
import com.yuchen.bearrecord.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserRepository userRepository;

    @Resource
    private DrugServiceImpl drugService;

    public Result login() {
        UserDto userDto = RequestHeaderContext.getInstance().getUserDto();
        HashMap<String, Object> map = new HashMap<>();

        User user = userRepository.findByOpenid(userDto.getOpenid());
        boolean userIsNew = user == null;
        log.info("user is new ? " + userIsNew);
        if (userIsNew) {
            if (saveNewUser(userDto)) {
                map.put("msg", "用户信息保存成功");
                map.put("isNewUser", true);
                return Result.ok(map);
            } else {
                return Result.error("用户信息保存失败");
            }

        } else {
            map.put("isNewUser", false);
            return Result.ok(map);
        }
    }

    @Override
    public boolean saveNewUser(UserDto userDto) {
        String openid = userDto.getOpenid();
        String appid = userDto.getAppid();
        if (openid != null && appid != null) {
            User user = new User();
            user.setOpenid(openid);
            user.setAppid(appid);
            user.setType(0);
            LocalDateTime now = LocalDateTime.now();
            user.setCreatetime(Timestamp.valueOf(now));
            userRepository.save(user);
            return true;
        }
        return false;
    }

    @Override
    public Result getUserType() {
        UserDto userDto = RequestHeaderContext.getInstance().getUserDto();
        HashMap<String, Object> map = new HashMap<>();
        User user = userRepository.findByOpenid(userDto.getOpenid());
        map.put("type", user.getType());
        return Result.ok(map);
    }

    @Override
    public Result getUserList() {
        UserDto userDto = RequestHeaderContext.getInstance().getUserDto();
        HashMap<String, Object> map = new HashMap<>();
        List<UserOpenidDto> openidList;
        if (drugService.checkProAdmin()) {
            if(drugService.checkAdmin()){
                return Result.error("当前用户不是管理员！");
            }
            openidList = userRepository.findUsers();
        }else{
            openidList = userRepository.findAllExcept(userDto.getOpenid());
        }
        map.put("userList", openidList);
        return Result.ok(map);
    }

    @Override
    public Result updateUserType(User user) {
        if (drugService.checkAdmin()) return Result.error("当前用户不是管理员！");
        try {
            User userByOpenid = userRepository.findByOpenid(user.getOpenid());
            userByOpenid.setType(user.getType());
            userRepository.save(userByOpenid);
        } catch (Exception e) {
            return Result.error(user.getOpenid() + "修改失败");
        }
        return Result.ok();
    }

    @Override
    public Result deleteUser(String openid) {
        if (drugService.checkAdmin()) return Result.error("当前用户不是管理员！");
        try {
            User byOpenid = userRepository.findByOpenid(openid);
            if (byOpenid.getType() == 1) {
                return Result.error("不能删除管理员，请先将其改为用户！");
            }
            userRepository.deleteByOpenid(openid);
        } catch (Exception e) {
            return Result.error(openid + "删除失败");
        }
        return Result.ok();
    }
}
